import { getExerciseData, addExerciseData, operate } from './service';
import { message } from 'antd';
import moment from 'moment';
const creator = (type, payload) => ({ type, payload });
export default {
  namespace: 'exercise',
  state: {
    exerciseData: [],
    legendData: [], //示例数据
    xData: [], //x轴数据
    serName1: '', //里程
    serName2: '', //耗时
    mileArr: [],
    timeArr: [],
  },
  effects: {
    //获取日记列表
    *getExerciseData({ payload }, { call, put }) {
      const res = yield call(getExerciseData, payload);
      const { type } = payload;
      if (res.httpStatus === 200) {
        const data = res.data;

        let xData = [];
        let mileArr = [];
        let timeArr = [];
        let mileage = 0;
        let time = 0;
        let serName1 = '里程(km)';
        let serName2 = '耗时(min)';

        if (type === 'all' || type === 'year') {
          //数据处理

          xData = [
            '一月',
            '二月',
            '三月',
            '四月',
            '五月',
            '六月',
            '七月',
            '八月',
            '九月',
            '十月',
            '十一月',
            '十二月',
          ];

          for (let i = 0; i < 12; i++) {
            //每个月的起始
            let start = moment()
              .month(i)
              .startOf('month');
            //每个月的结束
            let end = moment()
              .month(i)
              .endOf('month');
            mileage = 0; //每次循环前清空
            time = 0;
            data.forEach(item => {
              if (moment(item.start).isBetween(start, end)) {
                mileage += item.mileage;
                time += item.time;
              }
            });
            mileArr.push(mileage);
            timeArr.push(time);
          }
        } else if (type === 'week') {
          xData = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
          for (let i = 0; i < 7; i++) {
            //每周的的起始
            let start = moment()
              .weekday(i)
              .startOf('day');
            //每周的结束
            let end = moment()
              .weekday(i)
              .endOf('day');
            mileage = 0; //每次循环前清空
            time = 0;
            data.forEach(item => {
              if (moment(item.date).isBetween(start, end)) {
                mileage += item.mileage;
                time += item.time;
              }
            });
            mileArr.push(mileage);
            timeArr.push(time);
          }
        } else if (type === 'month') {
          let total=moment().daysInMonth()
          for (let i = 1; i < total+1; i++) {
            //每月的的起始
            let start = moment()
              .date(i)
              .startOf('day');
            //每月的结束
            let end = moment()
              .date(i)
              .endOf('day');
            let evevyday = moment(start).format('MM-DD');
            xData.push(evevyday);
            mileage = 0; //每次循环前清空
            time = 0;
            data.forEach(item => {
              if (moment(item.date).isBetween(start, end)) {
                mileage += item.mileage;
                time += item.time;
              }
            });
            mileArr.push(mileage);
            timeArr.push(time);
          }
        }
        yield put(
          creator('saveExerciseList', { data, serName1, serName2, xData, mileArr, timeArr }),
        );
      }
    },
    *addExerciseData({ payload }, { call, put }) {
      const res = yield call(addExerciseData, payload);
      return res;
    },
  },
  reducers: {
    saveExerciseList(state, { payload }) {
      const { data, serName1, serName2, xData, mileArr, timeArr } = payload;
      return {
        ...state,
        exerciseData: data,
        serName1,
        serName2,
        xData,
        mileArr,
        timeArr,
      };
    },
    saveData(state, { payload }) {
      const { legendData, seriesData } = payload;
      return {
        ...state,
        legendData,
        seriesData,
      };
    },
  },
};
